/**
 * @desc: The implement class,via it,you can get The implement class's
 *         information,Inc name,type and so on.
 * @author: cxlh
 */
package com.ids.transfer.config;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ids.transfer.data.XmlParse;

/**
 * The implement class,via it,you can get The implement class's information,Inc
 * name,type and so on.
 */
public class ItemConfig {
	protected HashMap properties = new HashMap();
	private StringBuffer sb = new StringBuffer();
	private Log logger = LogFactory.getLog(getClass());
	//item执行次数
	private int exetimes = 0;
	
	/**
	 * 
	 * @return
	 */
	public Map getProperties() {
		return (this.properties);
	}

	/**
   * 
   */
	private ItemConfig() {
	}

	/**
	 * 构造函数
	 * 
	 * @param cid
	 *            实现类名称
	 */
	public ItemConfig(String cid) {
		try {
			properties = XmlParse.getImpClassInfo(Constant.XMLFILEPATH, cid);
			if(!check()){
				logger.error("item解释出错，原因:"+sb.toString());
				System.exit(1);
			}
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
	}

	/**
	 * 检测合法性
	 * 
	 * @return
	 */
	private boolean check() // 校验TC创建中是否合法
	{
		// 检查必要的信息是否都已设定
		boolean flag = true;
		if (this.getName().equals("")) {
			sb.append("名称必须设定");
			return false;
		}
		if (this.getType().equals("")) {
			sb.append("Type属性必须设定");
			return false;
		}
		return flag;
	}
	
	/**
	 * 得到类的全路径信息
	 * 
	 * @return
	 */
	public String getType() {
		return (String) this.properties.get("type");
	}

	/**
	 * 得到实现类的所有模块的具体信息
	 * 
	 * @return HashMap
	 */
	public HashMap getPmap() {
		return (HashMap) this.properties.get("pmap");
	}

	/**
	 * 得到实现类名称
	 * 
	 * @return
	 */
	public String getName() {
		return (String) this.properties.get("name");
	}

	/**
	 * 得到实现类模块数目
	 * 
	 * @return
	 */
	public int getModuleNum() {
		return ((Integer) this.properties.get("modulenum")).intValue();
	}

	/**
	 * 类执行次数自加1
	 */
	public void addExeTimes() {
		exetimes++;
	}

	/**
	 * 获得此类总的执行次数
	 * 
	 * @return
	 */
	public int getExeTimes() {
		return exetimes;
	}

	/**
	 * 重载基类的toString函数
	 * 
	 * @return
	 */
	public String toString() {
		StringBuffer sb = new StringBuffer("CLASSINFO[");
		Iterator names = properties.keySet().iterator();
		while (names.hasNext()) {
			String name = (String) names.next();
			String value = properties.get(name).toString();
			sb.append(name);
			sb.append('=');
			sb.append(value);
			sb.append(',');
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append("]");
		return (sb.toString());
	}

	public static void main(String[] args) {
		try {
			ItemConfig tc = new ItemConfig("classa");
			System.out.print(tc);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}